﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace W6
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public void merge()                  //合并文件
        {

            FileStream fin1 = null;
            StreamReader brin1 = null;

            FileStream fin2 = null;
            StreamReader brin2 = null;


            FileStream fout = null;
            StreamWriter brout = null;
            try
            {
                String newPath = "D:\\C#\\6\\hw6\\" + "\\Data";
                if (!Directory.Exists(newPath))
                {
                    Directory.CreateDirectory(newPath);    // 如果目录不存在，则新建子目录
      
                }
                String path1 = "D:\\C#\\6\\hw6\\" + File1.SelectedItem.ToString();
                String path2 = "D:\\C#\\6\\hw6\\" + File2.SelectedItem.ToString();


                fin1 = new FileStream(path1, FileMode.Open, FileAccess.Read);
                fin2 = new FileStream(path2, FileMode.Open, FileAccess.Read);
                fout = new FileStream(newPath + "\\newText.txt", FileMode.Create, FileAccess.Write);  

                brin1 = new StreamReader(fin1, System.Text.Encoding.Default);
                brin2 = new StreamReader(fin2, System.Text.Encoding.Default);
                brout = new StreamWriter(fout, System.Text.Encoding.Default);



                for (String s = brin1.ReadLine(); s != null; s = brin1.ReadLine())
                {
                    brout.WriteLine(s);          //写入第一个文件
                }
                brin1.Close();
                for (String s = brin2.ReadLine(); s != null; s = brin2.ReadLine())
                {
                    brout.WriteLine(s);
                }                             //写入第二个文件
                brin2.Close(); 
                brout.Close();             //关闭流
                MessageBox.Show("已完成");

            }
            catch (FileNotFoundException filenotfound)
            {
                MessageBox.Show("file not found");
            }
            
     
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.merge();
        }
    }
}
